import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import wcome from '../components/wcome'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login},
  { 
    path: '/home', 
    component: Home,
    redirect: '/wcome',
    children: [
      {
        path: '/wcome', 
        component: wcome
      },
      {
        path: '/users', 
        component: () => import('../components/user')
      },
      {
        path: '/roles', 
        component: () => import('../components/power/roles')
      },
      {
        path: '/rights', 
        component: () => import('../components/power/rights')
      },
      {
        path: '/goods', 
        component: () => import('../components/goods/list')
      },
      {
        path: '/params', 
        component: () => import('../components/goods/params')
      },
      {
        path: '/categories', 
        component: () => import('../components/goods/cate')
      },
      {
        path: '/goods/add', 
        component: () => import('../components/goods/add')
      },
      {
        path: '/orders', 
        component: () => import('../components/order')
      },
      {
        path: '/reports', 
        component: () => import('../components/report')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if(to.path === '/login') return next();
  const token = window.sessionStorage.getItem('token');
  if(!token) return next('/login');
  next()
})


export default router
